package com.ticket.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ticket.entity.Feedback;
import com.ticket.mapper.FeedbackMapper;
import com.ticket.service.IFeedbackService;
import com.ticket.vo.FeedbackVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class FeedbackServiceImpl extends ServiceImpl<FeedbackMapper, Feedback> implements IFeedbackService {

    @Override
    public List<Feedback> getUserFeedbacks(Long userId) {
        return this.lambdaQuery()
            .eq(Feedback::getUserId, userId)
            .orderByDesc(Feedback::getCreatedAt)
            .list();
    }

    @Override
    @Transactional
    public void createFeedback(Long userId, FeedbackVO feedbackVO) {
        Feedback feedback = new Feedback();
        feedback.setUserId(userId);
        feedback.setContent(feedbackVO.getContent());
        feedback.setImageUrl(feedbackVO.getImageUrl());
        feedback.setStatus("PENDING");
        feedback.setCreatedAt(LocalDateTime.now());
        
        this.save(feedback);
    }

    @Override
    public List<Feedback> getRecentFeedbacks(Long userId, int limit) {
        LambdaQueryWrapper<Feedback> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Feedback::getUserId, userId)
                   .orderByDesc(Feedback::getCreatedAt)
                   .last("LIMIT " + limit);
        
        return this.baseMapper.selectList(queryWrapper);
    }
} 